package com.rs.controller;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.rs.model.CommonResult;
import com.rs.model.ZhItemsRecipients;
import com.rs.model.ZhOvertime;
import com.rs.model.ZhUser;
import com.rs.service.AuditInformationService;
import com.rs.util.DateFormatUtil;
import com.rs.util.SpringContextUtils;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ItemsRecipientsAudit extends JFrame {
	
	private int id;//用户id
	private ZhUser user;
	SelectItemsRecipients it;
	ImageIcon ico;
	public Integer kk=null;

	private JPanel contentPane;
	private JTextField textField_username;
	private JTextField textField_name;
	private JTextField textField_num;
	private JTextField textField_time;
	private JTextPane txtpnGg_detailed;
	private JLabel lblNewLabel_img;
	private JTextPane textPane_use;
	
	private AuditInformationService audit = SpringContextUtils.getBean(AuditInformationService.class);

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ItemsRecipientsAudit frame = new ItemsRecipientsAudit();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ItemsRecipientsAudit() {
		aa();
	}
	
	public ItemsRecipientsAudit(int id,ZhUser user,SelectItemsRecipients it) {
		this.id=id;
		this.it=it;
		this.user=user;
		aa();
	}
	
	void aa(){
	//	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 574, 509);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label_username = new JLabel("姓名：");
		label_username.setBounds(40, 83, 54, 15);
		contentPane.add(label_username);
		
		JLabel label_name = new JLabel("物品名称：");
		label_name.setBounds(40, 142, 67, 15);
		contentPane.add(label_name);
		
		JLabel label_num = new JLabel("领用数量：");
		label_num.setBounds(40, 196, 67, 15);
		contentPane.add(label_num);
		
		JLabel label_recipientsTime = new JLabel("领用时间：");
		label_recipientsTime.setBounds(40, 256, 67, 15);
		contentPane.add(label_recipientsTime);
		
		JLabel label_detailed = new JLabel("领用详情：");
		label_detailed.setBounds(40, 317, 67, 15);
		contentPane.add(label_detailed);
		
		JLabel label_use = new JLabel("物品用途：");
		label_use.setBounds(273, 83, 67, 15);
		contentPane.add(label_use);
		
		JLabel label_img = new JLabel("物品图片：");
		label_img.setBounds(273, 196, 67, 15);
		contentPane.add(label_img);
		
		textField_username = new JTextField();
		textField_username.setBounds(114, 80, 132, 21);
		contentPane.add(textField_username);
		textField_username.setColumns(10);
		
		textField_name = new JTextField();
		textField_name.setColumns(10);
		textField_name.setBounds(114, 139, 132, 21);
		contentPane.add(textField_name);
		
		textField_num = new JTextField();
		textField_num.setColumns(10);
		textField_num.setBounds(114, 193, 132, 21);
		contentPane.add(textField_num);
		
		textField_time = new JTextField();
		textField_time.setColumns(10);
		textField_time.setBounds(114, 253, 132, 21);
		contentPane.add(textField_time);
		
		txtpnGg_detailed = new JTextPane();
		txtpnGg_detailed.setBounds(114, 311, 132, 126);
		contentPane.add(txtpnGg_detailed);
		
		ico=new ImageIcon("src/main/java/images/null.jpg");
		ico.setImage(ico.getImage().getScaledInstance(135,95,Image.SCALE_DEFAULT));
		
		lblNewLabel_img = new JLabel(ico);
		lblNewLabel_img.setBounds(347, 196, 135,95);
		contentPane.add(lblNewLabel_img);
		
		textPane_use = new JTextPane();
		textPane_use.setBounds(283, 108, 199, 64);
		contentPane.add(textPane_use);
		
		JLabel label = new JLabel("物品审核");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("宋体", Font.PLAIN, 16));
		label.setBounds(221, 22, 91, 30);
		contentPane.add(label);
		
		JButton btnNewButton = new JButton("驳回");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				auditOvertime(2);
			}
		});
		btnNewButton.setBounds(286, 367, 81, 35);
		contentPane.add(btnNewButton);
		
		JButton button = new JButton("通过");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				auditOvertime(1);
			}
		});
		button.setBounds(387, 367, 81, 35);
		contentPane.add(button);
		
		setTheValue(geiIdIt());
	}
	
	//得到用户详细
	private ZhItemsRecipients geiIdIt(){
		return audit.getIdZhItemsRecipients(id);
	}
	
	//渲染数据
	private void setTheValue(ZhItemsRecipients it){
		textField_username.setText(it.getItUname());
		textField_name.setText(it.getIt_name());
		textField_num.setText(it.getIt_num()+"");
		textField_time.setText(DateFormatUtil.getDateStringw(it.getIt_time(), "yyyy.MM.dd HH:mm"));
		txtpnGg_detailed.setText(it.getIt_purpose());
		textPane_use.setText(it.getIt_reason());
		if(it.getIt_img() !=null){
			ico.setImage(Toolkit.getDefaultToolkit().getImage(it.getIt_img()));
			ico.setImage(ico.getImage().getScaledInstance(135,95,Image.SCALE_DEFAULT));
		}
		
		textField_username.setEditable(false);
		textField_name.setEditable(false);
		textField_num.setEditable(false);
		textField_time.setEditable(false);
		txtpnGg_detailed.setEditable(false);
		textPane_use.setEditable(false);
	}
	
	//审核 通过或驳回
	private void auditOvertime(Integer ovaudit){
		ZhItemsRecipients o= new ZhItemsRecipients();
		o.setIt_id(id);
		o.setIt_auid(user.getuId());
		o.setIt_audit(ovaudit);
		CommonResult<Integer> com  = audit.updateItemsRecipientsAudit(o);
		if(com.getData()>0){
			alert("操作成功！");
			kk =it.dynamicRefresh(id,kk);
		}
	}

	private void alert(String aa) {
		JOptionPane.showMessageDialog(this, aa);
	}
	
}
